package dao.Impl;

import dao.PetStoreDao;
import entity.PetOwner;
import entity.PetStore;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author wfr
 * @description:
 * @date 2019-05-31 15:13
 * @parem PetStoreDaoImpl
 */
public class PetStoreDaoImpl extends BaseDao  implements PetStoreDao {
    public PetStoreDaoImpl() {super();
    }

    @Override
    public int add(PetStore petOwner) throws SQLException {
        int r=0;
        try {

            PreparedStatement pst =con.prepareStatement("insert into petstore(NAME,PASSWORD,BALANCE) value (?,?,?)");
            pst.setString(1,petOwner.getName());
            pst.setString(2,petOwner.getPassword());
            pst.setInt(3,petOwner.getBalance());

            r=pst.executeUpdate();
        }catch (SQLException e)
        {
            e.printStackTrace();
        }

        return r;
    }

    @Override
    public PetStore getBtId(int id) {
        PetStore petStore=null;
        try {

            ResultSet resultSet;
            resultSet = con.createStatement().executeQuery("select * from petstore where ID="+id);
            if (resultSet.next())
            {
                petStore=new PetStore();
                petStore.setId(resultSet.getInt(1));
                petStore.setName(resultSet.getString(2));
                petStore.setPassword(resultSet.getString(3));
                petStore.setBalance(resultSet.getInt(4));

            }


        }catch (SQLException e)
        {
            e.printStackTrace();
        }
        return petStore;
    }

    @Override
    public List<PetStore> getlist() {
        List<PetStore> petStores = new ArrayList<PetStore>();
        try {
            String sql = "select * from petstore";
           PetStore petStore = null;
            PreparedStatement pst = con.prepareStatement(sql);
            // 设置条件(相对于条件查询)
            ResultSet resultSet;
            resultSet = con.createStatement().executeQuery(sql);

            while (resultSet.next()) {
                petStore = new PetStore();
                petStore.setId(resultSet.getInt(1));
                petStore.setName(resultSet.getString(2));
                petStore.setPassword(resultSet.getString(3));
                petStore.setBalance(resultSet.getInt(4));
                petStores.add(petStore);
            }

        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return petStores;
    }

    @Override
    public int updata(int id, int balence) {
        String sql = "update petstore set BALANCE=? where ID=?";

        PreparedStatement pst;
        try {
            pst = con.prepareStatement(sql);
            pst.setInt(1, balence);
            pst.setInt(2, id);
            pst.executeUpdate();
            System.out.println("更新成功");
            return 1;
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return 0;

    }
    @Override
    public int delete(int id) {
        String sql = "delete from petstore where ID=?";

        PreparedStatement pst;
        try {
            pst = con.prepareStatement(sql);
            pst.setInt(1, id);
            pst.executeUpdate();
            System.out.println("删除成功");
            return 1;
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return 0;
    }

    @Override
    public PetStore login(int account, String password) {
        PetStore petStore = null;
        try {

            ResultSet resultSet;
            String sql = "select * from petstore where ID=? and PASSWORD=?";
            PreparedStatement pst;
            pst = con.prepareStatement(sql);
            pst.setInt(1, account);
            pst.setString(2, password);
            resultSet = pst.executeQuery();
            if (resultSet.next()) {
                petStore = new PetStore();
                petStore.setId(resultSet.getInt(1));
                petStore.setName(resultSet.getString(2));
                petStore.setPassword(resultSet.getString(3));
                petStore.setBalance(resultSet.getInt(4));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return petStore;
    }
}
